package simmar.input;

import ch.idsia.benchmark.mario.environments.Environment;

public class InputKnowledge {
	boolean firstUpdate = true;
	
	float[] marioCurrentPos = new float[2];
	float[] marioLastPos = new float[2];
	float[] marioSpeed = new float[2];
	
	boolean marioCanShoot = false;
	
	boolean marioCanJump = false;
	boolean marioJumping = false;
	int marioJumpCounter = 0;
	final static int marioJumpLimit = 9;
	final static int marioJumpMin = 6;
	
	public void update(Environment environment) {
		// If this is the first update, make last step's info equal to current step
		if (firstUpdate) {
			marioCurrentPos = environment.getMarioFloatPos();
			marioLastPos[0] = marioCurrentPos[0];
			marioLastPos[1] = marioCurrentPos[1];

			firstUpdate = false;
		} else {
			marioLastPos[0] = marioCurrentPos[0];
			marioLastPos[1] = marioCurrentPos[1];
			
			marioCurrentPos = environment.getMarioFloatPos();
			
			marioSpeed[0] = marioCurrentPos[0] - marioLastPos[0];
			marioSpeed[1] = marioCurrentPos[1] - marioLastPos[1];
			
			marioCanShoot = environment.isMarioAbleToShoot();
			
			marioCanJump = environment.isMarioAbleToJump();
			
			if (marioJumping) {
				if (environment.isMarioOnGround()) {
					marioJumping = false;
					marioJumpCounter = 0;
				} else {
					marioJumpCounter++;
				}
			}
		}
	}
	
	public void reset() {
		firstUpdate = true;
		
		marioCurrentPos[0] = 0.0f;
		marioCurrentPos[1] = 0.0f;
		marioLastPos[0] = 0.0f;
		marioLastPos[1] = 0.0f;
		marioSpeed[0] = 0.0f;
		marioSpeed[1] = 0.0f;
	}
	
	public void jumpStart() {
		if (marioCanJump) {
			marioJumping = true;
		}
	}
}
